/**
 * 
 */
package com.zaprit.search.service.impl;

import java.util.List;

import javax.jms.JMSException;

import com.rdo.common.bo.Attachment;
import com.rdo.util.context.Context;
import com.rdo.util.event.queue.EventQueuePublisher;
import com.rdo.util.event.queue.MultiEventObject;
import com.rdo.util.search.FacetedSearchCriteria;
import com.rdo.util.search.FacetedSearchResult;
import com.zaprit.common.exception.CommonException;
import com.zaprit.common.service.CommonService;
import com.zaprit.search.bo.Item;
import com.zaprit.search.data.SearchEvent;
import com.zaprit.search.exception.SearchException;
import com.zaprit.search.service.SearchService;
import com.zaprit.search.support.ZapritSearcher;

/**
 * @author Root
 */
public class SearchServiceImpl implements SearchService
{
	private ZapritSearcher	zapritSearcher	= null;

	/**
	 * set the zapritSearcher loadedd from class path
	 * 
	 * @param zapritSearcher
	 */
	public void setZapritSearcher(ZapritSearcher zapritSearcher)
	{
		this.zapritSearcher = zapritSearcher;
	}

	public FacetedSearchResult<List<Item>> searchItem(String scopeId, String userId, FacetedSearchCriteria<String> facetedSearchCriteria)
			throws SearchException
	{
		try
		{
			return zapritSearcher.searchItems(scopeId, userId, facetedSearchCriteria);
		}
		catch (SearchException e)
		{
			throw new SearchException(e);
		}
	}

	public void addUpdateItem(String scopeId, String userId, Item item) throws SearchException
	{

	}

	public void triggerFileParsing(String scopeId, String userId, String attachmentId) throws SearchException
	{
		CommonService commonService = (CommonService) Context.getBean("commonService");
		try
		{
			Attachment attachment = commonService.getAttachment(scopeId, userId, attachmentId);
			EventQueuePublisher eventQueuePublisher = (EventQueuePublisher) Context.getBean("eventQueuePublisher");
			MultiEventObject multiEventObject = new MultiEventObject(SearchEvent.PARSE.toString(), attachment);
			eventQueuePublisher.publishEvent(multiEventObject);
		}
		catch (JMSException | CommonException e)
		{
			throw new SearchException(e);
		}
	}

	public Item parseFile(String scopeId, String userId, String attachmentId) throws SearchException
	{
		return null;
	}

	public Item getItem(String scopeId, String userId, String itemId) throws SearchException
	{
		return null;
	}

	public void rebuildIndex() throws SearchException
	{

	}

	public void rebuildIndex(String scopeId) throws SearchException

	{

	}
}
